﻿/// <reference path="../../Scripts/jquery-1.3.2.min-vsdoc.js" />
$(function() {
    $(".containerPlus").buildContainers();

    /*Sign Out Functions _________________________________________________________________________________*/

    function SignOutFunction() {
        $.ajax({
            type: "POST",
            url: "/Account/SignOut",
            dataType: "json",
            success: function(response) {
                if (response == "Succes") {
                    $("body").load(jQuery.url.attr("path"));
                }
                else {

                }
            }
        });
    };

    /*Sign In Functions _________________________________________________________________________________*/

    function SignInInit() {
        userName = $("#username").val();
        password = $("#password").val();
        rememberMe = $("#rememberMe").val();


        $('#Feed_List .News_feed').remove();
        $('#Feed_Header h2').hide();
        $('.ErrorMessage').remove('.ErrorMessage');
        $('.SuccesMessage').remove('.SuccesMessage');
        $('#LoadingImg').remove('#LoadingImg');

        $('<div id="LoadingImg"><img src="/Content/Images/Loading.gif" /></div>').appendTo('.containerPlus.LogInBox #LogInContent');

        SignInAjaxCall(userName, password, rememberMe);
    };

    function SignInOnSuccess(data) {
        if (data == "Succes") {
            LogInSuccesFunction();
        }
        else {
            for (x in data) {
                $('<div class="ErrorMessage"><br />' + data[x].Message + '</div>').appendTo('.containerPlus.LogInBox #LogInContent #LogInMessage');
            }
        }

        $('#LoadingImg').remove('#LoadingImg');

    };

    function SignInAjaxCall(userName, password, rememberMe) {

        $.ajax({
            type: "POST",
            url: "/Account/LogOn",
            data: "userName=" + userName + "&password=" + password + "&rememberMe=" + rememberMe,
            dataType: "json",
            success: SignInOnSuccess
        });
    };

    function LogInSuccesFunction() {
        $(".mbcontainercontent").hide();
        $("#PopUpMenu").hide();
        $(".MessageBox").fadeIn(100, function() {

            $(".MessageBox").html("Velkommen til danieltoft.dk!<br />Du er nu logget ind!");
            $(".containerPlus.LogInBox .n span").text("Information!");

            setTimeout(function() { $("body").load(jQuery.url.attr("path"), "POST"); }, 2000);
        });
    };


    /*Registration Functions _________________________________________________________________________________*/

    function RegisterInit() {
        username = $("#regUsername").val();
        email = $("#regEmail").val();
        password = $("#regPassword").val();
        confirmPassword = $("#regConfirmPassword").val();


        var CSSReturn = { "border-color": "#ccc", "background-color": "#fff" }
        $("#regUsername").css(CSSReturn);
        $("#regPassword").css(CSSReturn);
        $("#regConfirmPassword").css(CSSReturn);
        $("#regEmail").css(CSSReturn);

        $('.ErrorMessage').remove('.ErrorMessage');

        $('<div id="LoadingImg"><img src="/Content/Images/Loading.gif" /></div>').appendTo('.containerPlus.LogInBox #LogInContent');

        RegisterAjaxCall(username, email, password, confirmPassword);

    }

    function RegisterOnSucces(data) {
        if (data == "True") {
            UserCreated();
        }
        else {
            var failCSSColor = { "border-color": "#cc0000", "background-color": "#FFFFCC" };

            for (x in data) {
                if (data[x].Type == "password") {
                    $("#regPassword").css(failCSSColor);
                    $("#regConfirmPassword").css(failCSSColor);
                }
                if (data[x].Type == "username") {
                    $("#regUsername").css(failCSSColor);
                }
                if (data[x].Type == "email") {
                    $("#regEmail").css(failCSSColor);
                }


                $('<div class="ErrorMessage">' + x + ". " + data[x].Message + '</div>').appendTo('.containerPlus.LogInBox #CreateAccount .RegErrorMessageBox');
            }
        }

        $('#LoadingImg').remove('#LoadingImg');
    };

    function RegisterAjaxCall(username, email, password, confirmPassword) {
        $.ajax({
            type: "POST",
            url: "/Account/Register",
            data: "username=" + username + "&password=" + password + "&confirmPassword=" + confirmPassword + "&email=" + email,
            dataType: "json",
            success: RegisterOnSucces
        });
    };
    function UserCreated() {
        $(".containerPlus.LogInBox #CreateAccount").hide();
        $("#PopUpMenu").hide();
        $(".containerPlus.LogInBox .mbcontainercontent h3").hide();

        $(".MessageBox").fadeIn(100, function() {

            $(".containerPlus.LogInBox .n span").text("Information!");
            $(".MessageBox").html("Velkommen til danieltoft.dk!<br /><br />Der er nu oprettet en bruger med navnet " + $("#regUsername").val() + "<br /><br />Du vil om få sekunder blive logget ind med din nye bruger.");


            setTimeout(function() { $("body").load(jQuery.url.attr("path"), "POST"); }, 12000);
        });
    };

    /*Click Events _________________________________________________________________________________*/

    $("#btnSignIn").click(function() {
        $("#LogInbox").fadeIn();
        $("#btnLogInLink").addClass("PopUpMenuItemActive");
    });

    $(".lightBox").click(function() { $("#LogInbox").fadeOut() });

    $("#btnLogIn").click(function() { SignInInit(); });

    $("#btnSignOut").click(function() { SignOutFunction(); });

    $("#btnRegister").click(function() { RegisterInit(); });

    $("#btnLogInLink").click(function() {
        $(".containerPlus.LogInBox #CreateAccount").hide();

        $(".containerPlus.LogInBox #LogInContent").fadeIn(300);

        $(".containerPlus.LogInBox .mbcontainercontent h3").text("Log Ind!");

        $("#btnLogInLink").addClass("PopUpMenuItemActive");
        $("#btnRegistrerLink").removeClass("PopUpMenuItemActive");

    });
    $("#btnRegistrerLink").click(function() {
        $(".containerPlus.LogInBox #LogInContent").hide();

        $(".containerPlus.LogInBox #CreateAccount").fadeIn(300);

        $(".containerPlus.LogInBox .mbcontainercontent h3").text("Registrer")

        $("#btnRegistrerLink").addClass("PopUpMenuItemActive");
        $("#btnLogInLink").removeClass("PopUpMenuItemActive");
    });




});